﻿using System.Net.Mime;

namespace Uber.Service
{
    using System.Net;
    using System.Net.Mail;
    using Core.Service;

    public class MailSender : IMailSender
    {
        private readonly string serverName;
        private readonly string userName;
        private readonly string pass;


        public MailSender(ISmtpServerConfig smtpConfig)
        {
            serverName = smtpConfig.GetServerName();
            userName = smtpConfig.GetUserName();
            pass = smtpConfig.GetPass();

        }

        public void SendEmail(string from, string to, string subject, string body)
        {
            var message = new MailMessage(new MailAddress(from), new MailAddress(to))
                              {
                                  Subject = subject,
                                  Body = body,
                                  IsBodyHtml = true
                              };

            message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(body, new ContentType("text/html")));

            var client = new SmtpClient(serverName) {EnableSsl = true};
            var credentials = new NetworkCredential(userName, pass);
            client.Credentials = credentials;
            client.Send(message);
        }        
        
        public void SendEmailAsync(string from, string to, string subject, string body)
        {
            var message = new MailMessage(new MailAddress(from), new MailAddress(to))
            {
                Subject = subject,
                Body = body
            };

            var client = new SmtpClient(serverName) {EnableSsl = true};
            var credentials = new NetworkCredential(userName, pass);
            client.Credentials = credentials;
            client.SendAsync(message,0);
        }

    }
}
